package com.mall.service.marketing.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mall.feign.clients.CommodityClient;
import com.mall.model.domain.PmsProduct;
import com.mall.model.domain.SmsFlashPromotionProductRelation;
import com.mall.model.vo.PageResult;
import com.mall.model.vo.SmsFlashPromotionProductVo;
import com.mall.service.marketing.mapper.SmsFlashPromotionProductRelationMapper;
import com.mall.service.marketing.service.SmsFlashPromotionProductRelationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class SmsFlashPromotionProductRelationServiceImpl extends ServiceImpl<SmsFlashPromotionProductRelationMapper, SmsFlashPromotionProductRelation> implements SmsFlashPromotionProductRelationService {

    @Autowired
    private SmsFlashPromotionProductRelationMapper smsFlashPromotionProductRelationMapper;

    @Autowired
    private CommodityClient commodityClient;

    /**
     * 分页查询不同场次关联及商品信息
     *
     * @param flashPromotionId
     * @param flashPromotionSessionId
     * @param pageSize
     * @param pageNum
     * @return
     */
    @Override
    public PageResult listWithProduct(Long flashPromotionId, Long flashPromotionSessionId, Integer pageNum, Integer pageSize) {

        IPage<SmsFlashPromotionProductRelation> page = new Page<>(pageNum, pageSize);

        LambdaQueryWrapper<SmsFlashPromotionProductRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SmsFlashPromotionProductRelation::getFlashPromotionId, flashPromotionId)
                .eq(SmsFlashPromotionProductRelation::getFlashPromotionSessionId, flashPromotionSessionId);

        IPage<SmsFlashPromotionProductRelation> pageInfo = smsFlashPromotionProductRelationMapper.selectPage(page, queryWrapper);
        List<SmsFlashPromotionProductRelation> records = pageInfo.getRecords();
        //获取其中的productId
        List<Long> productIds = CollUtil.getFieldValues(records, "productId", Long.class);
        log.info("productIds={}", productIds);

        IPage<SmsFlashPromotionProductVo> voPage = new Page<>();
        BeanUtils.copyProperties(pageInfo, voPage, "records");

        List<SmsFlashPromotionProductVo> vos = new ArrayList<>();
        for (SmsFlashPromotionProductRelation record : records) {
            SmsFlashPromotionProductVo vo = new SmsFlashPromotionProductVo();
            BeanUtils.copyProperties(record, vo);
            PmsProduct pmsProduct = commodityClient.findById(record.getProductId());
            vo.setProduct(pmsProduct);
            vos.add(vo);
        }
        voPage.setRecords(vos);

        return new PageResult(pageNum, pageSize, (int) voPage.getTotal(), voPage.getRecords());
    }

    /**
     * 修改关联信息
     *
     * @param id
     * @param smsFlashPromotionProductRelation
     * @return
     */
    @Override
    public Integer update(Long id, SmsFlashPromotionProductRelation smsFlashPromotionProductRelation) {
        smsFlashPromotionProductRelation.setId(id);
        return smsFlashPromotionProductRelationMapper.updateById(smsFlashPromotionProductRelation);
    }
}
